package hotLeetcode;

import java.util.HashMap;
import java.util.Map;

public class H121Leetcode {
    public static int maxProfit1(int[] prices) {


        HashMap<Integer, Integer> map = new HashMap<>();
        int length = prices.length;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                map.put(prices[j] - prices[i], j);

            }
        }

        int ans = map.entrySet().stream().
                max(Map.Entry.comparingByKey())
                .filter(entry -> entry.getKey() >= 0)
                .map(Map.Entry::getKey)
                .orElse(0);


        return ans;



    }


    public static int maxProfit2(int[] prices) {
        int length = prices.length;

        int max = 0;
        int temp = 0;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {

                temp = prices[j] - prices[i];
                max = Math.max(max, temp);


            }

        }

        return max;


    }

    public static int maxProfit(int[] prices) {




        int minv = Integer.MAX_VALUE;
        int ans = 0;
        for (int price : prices) {
            ans = Math.max(ans, price - minv);
            minv = Math.min(minv, price);
        }

        return ans;

    }




    public static void main(String[] args) {
        int[] prices = {7,1,5,3,6,4};
        System.out.println(H121Leetcode.maxProfit(prices));




    }

}
